package com.rcloud.server.sealtalk.task;


import com.rcloud.server.sealtalk.constant.Constants;
import com.rcloud.server.sealtalk.controller.param.SendMessageContent;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.MiscManager;
import com.rcloud.server.sealtalk.model.ResultBean;
import com.rcloud.server.sealtalk.util.MultiThreadUtils;
import com.rcloud.server.sealtalk.util.N3d;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * 具体执行业务任务 需要 实现ITask接口  在execute中重写业务逻辑
 * TestTask<BR>
 * 创建人:wangbeidou <BR>
 * 时间：2018年8月8日-下午8:40:32 <BR>
 * @version 2.0
 *
 */
@Slf4j
@Component
public class NoticeForBackTask implements ITask<ResultBean<String>, Users> {

    @Resource
    private MiscManager miscManager;

    @SneakyThrows
    @Override
    public ResultBean execute(List<Users> data, Map<String, Object> params) {
        /**
         * 具体业务逻辑：将list中的元素加上辅助参数中的数据返回
         */
        List<String> strings = new ArrayList<>();
        for (Users user : data) {
            //判断是否是全局接收
            strings.add(N3d.encode(user.getId()));
        }
        //这里就执行发送的逻辑  todo 写死了 客服的账号和昵称
        try {
            log.info("执行------接口发送系统消息通知");
            log.info("执行中------接口strings的长度" + strings.size());
            String[] array2 = strings.toArray(new String[strings.size()]);
            SendMessageContent content = new SendMessageContent();
            content.setExtra(params.get("pushData").toString());
            content.setContent(params.get("publishNickname").toString() + "发布了一条商机，👉【点击查看】👈");
            miscManager.sendCommonTxtMessage(params.get("pushData").toString(), 4, Constants.objectName, content, "您有一条新的商机!", array2);
        } catch (ServiceException e) {
            log.error("出现异常------接口发送系统消息通知" + e);
        }
        ResultBean<List<Users>> resultBean = ResultBean.newInstance();
        resultBean.setData(data);
        return resultBean;
    }
}